from django.test import TestCase
from django.contrib.auth.models import User
# Create your tests here.
class SimpleTest(TestCase):
    fixtures = ['test_data.json','user.json']

    def createAuction(self):#UC3 Test
        # It will redirect the user to login page if he is not logged in and try to edit the bolg.
        response = self.client.post('/create/', {'title': 'Changed','content': 'Changed content'}, follow=True)
        self.assertEquals(response.status_code, 200)
        self.assertContains(response,"Log In")

        #Login with the user created
        loggedin = self.client.login(username='aman', password='aman')
        self.assertTrue(loggedin)#Checks if user is logged in is true then he can edit.

        # This will checks if the title is empty.
        response = self.client.post('/create/', {'title': '','category': 'Test Category','cPrice': '12','iDesc': 'Test Description','eDate':'2014-12-12 12:10', 'aOwner':'Test Owner'})
        self.assertEquals(response.status_code, 200)
        self.assertContains(response,"Title can not be empty.")

        # This will checks if the category is empty.
        response = self.client.post('/create/', {'title': 'Title 1','category': '','cPrice': '12','iDesc': 'Test Description','eDate':'2014-12-12 12:10', 'aOwner':'Test Owner'})
        self.assertEquals(response.status_code, 200)
        self.assertContains(response,"Category can not be empty.")

        # This will checks if the price entered is empty, 0 or less than 0.
        response = self.client.post('/create/', {'title': 'Title 1','category': 'chategory','cPrice': '','iDesc': 'Test Description','eDate':'2014-12-12 12:10', 'aOwner':'Test Owner'})
        response = self.client.post('/create/', {'title': 'Title 1','category': 'chategory','cPrice': '0','iDesc': 'Test Description','eDate':'2014-12-12 12:10', 'aOwner':'Test Owner'})
        response = self.client.post('/create/', {'title': 'Title 1','category': 'chategory','cPrice': '-1','iDesc': 'Test Description','eDate':'2014-12-12 12:10', 'aOwner':'Test Owner'})
        self.assertEquals(response.status_code, 200)
        self.assertContains(response,"Price should be number and can not be empty or 0.")

        # Description is possible to have a empty filed.
        response = self.client.post('/create/', {'title': 'Title 1','category': 'category','cPrice': '12','iDesc': '','eDate':'2014-12-12 12:10', 'aOwner':'Test Owner'})
        self.assertEquals(response.status_code, 200)
        self.assertContains(response,"Confirm Auction")

        # If the date format is not matched.
        response = self.client.post('/create/', {'title': 'Title 1','category': 'category','cPrice': '12','iDesc': 'Description','eDate':'20141212 12:10', 'aOwner':'Test Owner'})
        self.assertContains(response,"Date is either empty or does not match the format.")
        self.assertEquals(response.status_code, 200)

        #If time difference is less than 72 hours
        response = self.client.post('/create/', {'title': 'Title 1','category': 'category','cPrice': '12','iDesc': 'Description','eDate':'2014-10-12 12:10', 'aOwner':'Test Owner'})
        self.assertContains(response,"Minimum time for the auction should not be less than 72 hours.")
        self.assertEquals(response.status_code, 200)

        #if the date field is empty
        response = self.client.post('/create/', {'title': 'Title 1','category': 'category','cPrice': '12','iDesc': 'Description','eDate':'', 'aOwner':'Test Owner'})
        self.assertContains(response,"Date is either empty")
        self.assertEquals(response.status_code, 200)

        # If the owner field is empty.
        response = self.client.post('/create/', {'title': 'Test Title','category': 'Test Category','cPrice': '12','iDesc': 'Test Description','eDate':'2014-12-12 12:10', 'aOwner':''})
        self.assertEquals(response.status_code, 200)
        self.assertContains(response,"Owner field can not be empty.")

        # If all the fields are valid it will redirect the user to the confirmation page.
        response = self.client.post('/create/', {'title': 'Test Title','category': 'Test Category','cPrice': '12','iDesc': 'Test Description','eDate':'2014-12-12 12:10', 'aOwner':'Test Owner'})
        self.assertEquals(response.status_code, 200)
        self.assertContains(response,"Confirm Auction")

        #If the user selects No the auction will not be saved.
        resp = self.client.post('/saveauction/', {'t_title': 'Test Title','c_category': 'Test Category','c_Price': '12','i_Desc': 'Test Description','e_Date':'2014-12-12 12:10', 'a_Owner':'Test Owner', 'option':'No'})
        self.assertEqual(resp.status_code,200)
        self.assertContains(resp,"Aunction was not saved.")

        #If the user selects Yes the auction will be saved.
        resp = self.client.post('/saveauction/', {'t_title': 'Test Title','c_category': 'Test Category','c_Price': '12','i_Desc': 'Test Description','e_Date':'2014-12-12 12:10', 'a_Owner':'Test Owner', 'option':'Yes'})
        self.assertEqual(resp.status_code,200)
        self.assertContains(resp,"New post has been saved")
        self.client.logout()

    def bidAuction(self):#UC6
        # It will redirect the user to login page if he is not logged in and try to edit the bolg.
        resp = self.client.get('/item/1/')
        self.assertEqual(resp.status_code,200)
        self.assertContains(resp,"Log In to bid on this item.")

        # This will confirm the login
        loggedin = self.client.login(username='aman', password='aman')
        self.assertTrue(loggedin)

        # # This will occur if the user is already winning the auction
        resp = self.client.get('/item/1/')
        self.assertEqual(resp.status_code,200)
        self.assertContains(resp,"At the moment you are winning this product. So you cant bid.")
        self.client.logout()

        # This will confirm the login
        loggedin = self.client.login(username='rita', password='rita')
        self.assertTrue(loggedin)

        # If the user tries to bid with lower price than the minimum bid price the use will be show with a message to bid with the price more than the minimum bid price.
        resp = self.client.post('/item/1/', {'c_price':'10.12', 'edited_version':'4'})
        self.assertEqual(resp.status_code,200)
        self.assertContains(resp,"Minimum increase should be")

        # If the bid has been increased by other user before it was saved meaning the version was changed causing the conflict then it will show the conflict message
        resp = self.client.post('/item/1/', {'c_price':'1000', 'edited_version':'3'})
        self.assertEqual(resp.status_code,200)
        self.assertContains(resp,"The item description has been changed or the bid has been increased")

        # If the auction aowner is not the bider, the price is valid and no conflicts has occured then the user can successfully bid.
        resp = self.client.post('/item/1/', {'c_price':'571.12', 'edited_version':'4'})
        self.assertEqual(resp.status_code,200)
        self.assertContains(resp,"The bid for Auction 1 has been increased.")
        self.client.logout()

    def Concurrency(self):#UC10
        # It will redirect the user to login page if he is not logged in and try to edit the bolg.
        resp = self.client.get('/item/1/')
        self.assertEqual(resp.status_code,200)
        self.assertContains(resp,"Username:")

        # This will confirm the login
        loggedin = self.client.login(username='rita', password='rita')
        self.assertTrue(loggedin)

        # Once the user is logged in the item will display the auction in bidding mode
        #  meaning now the user can bid on it if the user is not the owner of the item or already winning the item.
        resp = self.client.get('/item/1/')
        self.assertEqual(resp.status_code,200)
        self.assertContains(resp,"Bid on this Item.")

        # If the bid description has been changed by the owner of the product before it was saved meaning the version
        #  was changed causing the conflict then it will show the conflict message
        resp = self.client.post('/item/1/', {'c_price':'1000', 'edited_version':'3'})
        self.assertEqual(resp.status_code,200)
        self.assertContains(resp,"The item description has been changed")

        # If the auction aowner is not the bider, the price is valid and no conflicts
        # has occured then the user can successfully bid.
        resp = self.client.post('/item/1/', {'c_price':'572.12', 'edited_version':'4'})
        self.assertEqual(resp.status_code,200)
        self.assertContains(resp,"The bid for Auction 1 has been increased.")
        self.client.logout()